#!/usr/bin/perl

package Data;

use strict;
use warnings;

use Exporter qw(import);
use JSON;
use File::Find;
use File::Basename;

use lib "./libs";
use Utils qw(dd);
use Utils;
use Log;
use Const;
use App;

sub connList
{
    my $param = shift @ARGV;
    my $conns;

    my $config_path = $Const::CONF_PATH . $Const::DS . "conns";
    my %conn_configs;
    find(sub {
        if (-f) {
            my $basename = basename($File::Find::name);
            unless ($basename =~ /\.json$/) {
                next;
            }
            $conn_configs{$basename} = decode_json(Utils::readFile($File::Find::name));
        }
    }, $config_path);

    Log::info("Data conn list:");
    for my $conn (keys %conn_configs) {
        Log::primary("Conn config: ${conn}");
        my %config = %{$conn_configs{$conn}};
        # dd($config{type});
        Log::info("    Type: $config{type}, Host: $config{host}, Port: $config{port}, Database: $config{database}, Schema: $config{schema}, User: $config{user}, Password: $config{pass}");
        Log::info("    Gsql: gsql -h $config{host} -p $config{port} -d $config{database} -U $config{user} -W'$config{pass}'");
    }
}

sub run
{
    my ($table, $src_conn, $dst_conn) = @ARGV;
    unless (defined($table) && defined($src_conn) && defined($dst_conn)) {
        Log::error("invalid command, using $0 data:run <table> <src_conn> <dst_conn>");
        exit 1;
    }
    $table = lc($table);
    
    my $config_path = $Const::CONF_PATH. $Const::DS. "conns";
    my %src_config = %{ decode_json(Utils::readFile($config_path. $Const::DS. "${src_conn}.json")) };
    my %dst_config = %{ decode_json(Utils::readFile($config_path. $Const::DS. "${dst_conn}.json")) };

    my $sql;
    my $row_count;

    Log::primary("Start migrate data for table: ${table}");

    my $foreign_table_name = "nt_migrate_${table}";
    my $foreign_table_exp_options = "SERVER gsmpp_server OPTIONS (DELIMITER '^|', ENCODING 'utf8', FORMAT 'text', HEADER 'false', NOESCAPING 'true', LOCATION 'gsfs://9.3.5.14:5000/exp_data/NT_MIGRATE/${table}/', \"null\" '')";
    my $foreign_table_imp_options = "SERVER gsmpp_server OPTIONS (DELIMITER '^|', ENCODING 'utf8', FORMAT 'text', HEADER 'false', NOESCAPING 'true', LOCATION 'gsfs://9.3.5.14:5000/exp_data/NT_MIGRATE/${table}/*', \"null\" '')";

    my $src_db = App::getDB(\%src_config);
    my $src_sth;

    Log::info("src db: $src_db->{config}{host}:$src_db->{config}{port}/$src_db->{config}{database}, $src_db->{dbh}");
    $sql = "DROP FOREIGN TABLE IF EXISTS $src_config{schema2}.${foreign_table_name}";
    Log::info("src db exec > $sql;");
    $src_sth = $src_db->{dbh}->prepare($sql);
    $src_sth->execute();
    $src_sth->finish();
    # $sql = "CREATE FOREIGN TABLE $src_config{schema2}.${foreign_table_name} (LIKE $src_config{schema}.${table}) ${foreign_table_exp_options} WRITE ONLY";
    $sql = "CREATE FOREIGN TABLE $src_config{schema2}.${foreign_table_name} (LIKE $src_config{schema}.${table}) ${foreign_table_exp_options} WRITE ONLY";
    Log::info("src db exec > $sql;");
    $src_sth = $src_db->{dbh}->prepare($sql);
    $src_sth->execute();
    $src_sth->finish();
    $sql = "INSERT INTO $src_config{schema2}.${foreign_table_name} SELECT * FROM $src_config{schema}.${table}";
    Log::info("src db exec > $sql;");
    $src_sth = $src_db->{dbh}->prepare($sql);
    $src_sth->execute();
    $src_sth->finish();
    $row_count = $src_sth->rows;
    Log::info("export data rows: ${row_count}");
    $sql = "DROP FOREIGN TABLE IF EXISTS $src_config{schema2}.${foreign_table_name}";
    Log::info("src db exec > $sql;");
    $src_sth = $src_db->{dbh}->prepare($sql);
    $src_sth->execute();
    $src_sth->finish();

    App::closeDB(\%src_config);

    my $dst_db = App::getDB(\%dst_config);
    my $dst_sth;

    Log::info("dst db: $dst_db->{config}{host}:$dst_db->{config}{port}/$dst_db->{config}{database}, $dst_db->{dbh}");
    $sql = "DROP FOREIGN TABLE IF EXISTS $dst_config{schema}.${foreign_table_name}";
    Log::info("dst db exec > $sql;");
    $dst_sth = $dst_db->{dbh}->prepare($sql);
    $dst_sth->execute();
    $dst_sth->finish();
    $sql = "CREATE FOREIGN TABLE $dst_config{schema}.${foreign_table_name} (LIKE $dst_config{schema}.${table}) ${foreign_table_imp_options} READ ONLY";
    Log::info("dst db exec > $sql;");
    $dst_sth = $dst_db->{dbh}->prepare($sql);
    $dst_sth->execute();
    $dst_sth->finish();
    $sql = "TRUNCATE TABLE $dst_config{schema}.${table}";
    Log::info("dst db exec > $sql;");
    $dst_sth = $dst_db->{dbh}->prepare($sql);
    $dst_sth->execute();
    $dst_sth->finish();
    $sql = "INSERT INTO $dst_config{schema}.${table} SELECT * FROM $dst_config{schema}.${foreign_table_name}";
    Log::info("dst db exec > $sql;");
    $dst_sth = $dst_db->{dbh}->prepare($sql);
    $dst_sth->execute();
    $dst_sth->finish();
    $row_count = $dst_sth->rows;
    Log::info("import data rows: ${row_count}");
    $sql = "DROP FOREIGN TABLE IF EXISTS $dst_config{schema}.${foreign_table_name}";
    Log::info("dst db exec > $sql;");
    $dst_sth = $dst_db->{dbh}->prepare($sql);
    $dst_sth->execute();
    $dst_sth->finish();
    $sql = "SELECT COUNT(*) FROM $dst_config{schema}.${table}";
    Log::info("dst db exec > $sql;");
    $dst_sth = $dst_db->{dbh}->prepare($sql);
    $dst_sth->execute();
    my $count = $dst_sth->fetchrow_array;
    Log::info("dst db data rows: ${count}");
    $dst_sth->finish();
    
    App::closeDB(\%dst_config);

    Log::warn("Run command on gds server: rm -rf /opt/input_data/exp_data/NT_MIGRATE/${table}/");

    Log::primary("End migrate data for table: ${table}");
}

1;


__END__
